package xperiment.metaphor.model.resource;

import java.io.Serializable;

/**
 *
 * @author Mark Ashworth
 * @version 1.0.0
 */
public class LocaleKey implements Serializable {
	public String language; 
    public String country;
	
	public LocaleKey() {
		super();
	}
	public LocaleKey(String code) {
		
		if (code == null) {
			language = null;
			country = null;
		} else if (code.trim().equals("")) {
			language = null;
			country = null;
		} else if (code.contains("_")) {
			String[] parts = code.split("_");
			if (parts.length == 1) {
				language = parts[0].trim();
				country = parts[1].toUpperCase().trim();
			}
		} else {
			language = code.trim();
		}
	}
	public LocaleKey(String language, String country) {
		this.language = language;
		this.country = country;
	}
	
	public String getCode() {
		String code = "";
		if (language != null) {
			code = code + language;
		}
		if (country != null) {
			if (code.length() > 0) {
				code = code + "_";
			}
			code = code + country;
		}
		return code;
	}
	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		if (country != null) {
			this.country = country.toUpperCase();
		} else {
			this.country = null;
		}
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	@Override public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof LocaleKey)) {
			return false;
		}
		LocaleKey c = (LocaleKey) obj;

		return getCode().equals(c.getCode());
	}

	@Override public int hashCode() {
		return this.getCode().hashCode();
	}

	@Override public String toString() {
		return getCode();
	}
}
